using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SetterWindowClass : MonoBehaviour{
	
	public UILabel description;
	public UITable inputHolder;
	public GameObject OK;
	public GameObject Cancel;
	
	List<UILabel> inputResults;
	List<SetterWindowDescription> refValues = new List<SetterWindowDescription>{};
	
	List<bool> booleanOut = new List<bool>{false};
	
	KeyValuePair<string, GameObject> callback;
	
	void Start()
	{
		//callback = new KeyValuePair<string, GameObject>("",null);
	}
	
	void Update()
	{
		
	}
	
	public void LoadSimpleConfirmation(string windowDescription, List<bool> output = null)
	{
		UIButtonMessage message = OK.AddComponent<UIButtonMessage>();
		message.functionName = "OKClicked";
		message.target = gameObject;
		
		message = Cancel.AddComponent<UIButtonMessage>();
		message.functionName = "CancelClicked";
		message.target = gameObject;
		
		if(output == null)
			Cancel.SetActiveRecursively(false);
		
		description.text = windowDescription;
		
		booleanOut = output;
	}
	
	public void LoadDescription(string windowDescription, List<SetterWindowDescription> variables, string callback = null, GameObject target = null)
	{
		inputResults = new List<UILabel>();
		refValues = new List<SetterWindowDescription>();
		
		UIButtonMessage message = OK.AddComponent<UIButtonMessage>();
		message.functionName = "OKClicked";
		message.target = gameObject;
		
		message = Cancel.AddComponent<UIButtonMessage>();
		message.functionName = "CancelClicked";
		message.target = gameObject;
		
		GameReference reference = GameObject.FindObjectOfType(typeof(GameReference)) as GameReference;
		
		description.text = windowDescription;
		int i = 0;
		
		if(callback != null)
		{
			this.callback = new KeyValuePair<string, GameObject>(callback, target);
			Cancel.SetActiveRecursively(false);
		}
		
		foreach(SetterWindowDescription desc in variables)
		{
			if(desc.isLarge && !desc.multiOptionList)
			{
				GameObject input = Instantiate(reference.SetterWindowLargeInputPrefab) as GameObject;
				input.name = "b_large_" + i;
				input.transform.parent = inputHolder.transform;
				input.transform.localScale = reference.SetterWindowLargeInputPrefab.transform.localScale;
				input.transform.GetChild(1).gameObject.GetComponent<UILabel>().text = desc.defaultValue as string;
				input.transform.GetChild(2).gameObject.GetComponent<UILabel>().text = desc.label as string;
				inputResults.Add(input.transform.GetChild(1).gameObject.GetComponent<UILabel>());
			}
			else if(!desc.multiOptionList)
			{
				GameObject input = Instantiate(reference.SetterWindowSmallInputPrefab) as GameObject;
				input.name = "a_small_" + i;
				input.transform.parent = inputHolder.transform;
				input.transform.localScale = reference.SetterWindowSmallInputPrefab.transform.localScale;
				input.transform.GetChild(1).gameObject.GetComponent<UILabel>().text = desc.defaultValue;
				input.transform.GetChild(2).gameObject.GetComponent<UILabel>().text = desc.label;
				inputResults.Add(input.transform.GetChild(1).gameObject.GetComponent<UILabel>());
			}
			else
			{
				GameObject input = Instantiate(reference.SetterWindowMultiOptionListPrefab) as GameObject;
				input.name = "c_multi_" + i;
				input.transform.parent = inputHolder.transform;
				input.transform.localScale = reference.SetterWindowSmallInputPrefab.transform.localScale;
				
				GameObject baseButton = input.transform.GetChild(0).gameObject;
				
				List<GameObject> buttons = new List<GameObject>();
				
				foreach(string option in desc.options)
				{
					GameObject g = Instantiate(baseButton) as GameObject;
					g.transform.parent = inputHolder.transform;
					g.transform.localScale = Vector3.one;
					UIDragObject d = g.GetComponent<UIDragObject>();
					d.target = inputHolder.transform;
					g.transform.GetChild(1).GetComponent<UILabel>().text = option;
					g.name = option;
					UIButtonMessage m = g.AddComponent<UIButtonMessage>();
					m.target = gameObject;
					m.functionName = "optionSelected";
					buttons.Add(g);
				}
				
				Destroy(input);
				
				if(!desc.canSelectMultiple)
				{
					foreach(GameObject buttonOption1 in buttons)
					{
						foreach(GameObject buttonOption2 in buttons)
						{
							if(buttonOption1.name != buttonOption2.name)
							{
								UIButtonTween untweener = buttonOption1.AddComponent<UIButtonTween>();
								untweener.tweenTarget = buttonOption2.transform.GetChild(0).gameObject;
								untweener.playDirection = AnimationOrTween.Direction.Reverse;
							}
						}
					}
				}
				else
				{
					foreach(GameObject buttonOption1 in buttons)
					{
						UIButtonTween tweener = buttonOption1.GetComponent<UIButtonTween>();
						tweener.playDirection = AnimationOrTween.Direction.Toggle;
						UIButtonMessage m = buttonOption1.GetComponent<UIButtonMessage>();
						m.functionName = "optionToggled";
					}
				}
					
				refValues.Add(desc);
				inputHolder.repositionNow = true;
				return;
				
			}
			refValues.Add(desc);
			i++;
		}
		
		inputHolder.repositionNow = true;
	}
	
	void optionSelected(GameObject button)
	{
		Debug.Log(button.name);
		refValues[0].outVariable = button.name;
		
		if(!string.IsNullOrEmpty(callback.Key))
		{
			callback.Value.SendMessage(callback.Key, button.name, SendMessageOptions.DontRequireReceiver);
			button.SendMessage("Play", false, SendMessageOptions.DontRequireReceiver);
		}
	}
	
	void optionToggled(GameObject button)
	{
		List<string> jsonSelections = (MiniJSON.Json.Deserialize(refValues[0].outVariable) as List<object>).ConvertAll<string>(o => o as string);
		if(jsonSelections.Contains(button.name))
			jsonSelections.Remove(button.name);
		else
			jsonSelections.Add(button.name);
		
		refValues[0].outVariable = MiniJSON.Json.Serialize(jsonSelections);
	}
	
	void OKClicked()
	{
		if(refValues.Count > 0)
		if(!refValues[0].multiOptionList)		
			for(int i = 0; i < refValues.Count; i++)
			{
				refValues[i].outVariable = inputResults[i].text;
			}
		
		if(booleanOut != null)
		{
			booleanOut[0] = true;
		}
		
		Destroy(gameObject);
	}
	
	void CancelClicked()
	{	
		if(booleanOut != null)
		{
			booleanOut[0] = false;
		}
		
		if(refValues.Count > 0)
		refValues[0].outVariable = null;
		
		Destroy(gameObject);
	}
	
}
